package main

import (
	"fmt"
	"sort"
	"math/rand"
)

type Hero struct{
	Name string 
	Age int 
}
//结构体切片  【结构体数组】
type Heros []Hero 

//Len,Less,Swap 实现了sort 包里的Sort函数 
//https://studygolang.com/static/pkgdoc/pkg/sort.htm#Sort
//func Sort(data Interface)  Interface 接口有Len,Less,Swap方法
func (hs Heros)Len()int{
	return len(hs)
}

func (hs Heros)Less(i,j int)bool{
	return hs[i].Age>hs[j].Age 
}

func (hs Heros)Swap(i,j int){
	// temp:=hs[i]
	// hs[i]=hs[j]
	// hs[j]=temp 
	hs[i],hs[j]=hs[j],hs[i]
}
//type IntSlice []int
//接口切片排序
func main(){

	intSlice:=[]int{1,-1,3,2,8}

	sort.Ints(intSlice)

	fmt.Println(intSlice)

	//切片  里面元素都是结构体  
	var heros Heros

	for i:=0;i<10;i++{

		hero:=Hero{
			Name:fmt.Sprintf("hero%d",rand.Intn(100)),
			Age:rand.Intn(100),
		}

		heros = append(heros,hero)
	}

	for _,v:=range heros{
		fmt.Println(v)
	}

	fmt.Println("****************")

	sort.Sort(heros)

	for _,v:=range heros{

		fmt.Println(v)
	}

	fmt.Println("*************")
	var j sort.IntSlice = sort.IntSlice{1,2,3,4}

	fmt.Println(j.Len())

}